home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 1999 #5 / 1999 CD 5 (black).iso / Delphi3 / install / data.z / GRIDS.INT < prev    next >
Encoding:
Text File  |  1997-08-05  |  18.8 KB  |  473 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Delphi Visual Component Library                 }
  5. {                                                       }
  6. {       Copyright (c) 1995,97 Borland International     }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit Grids;
  11.  
  12. {$R-}
  13.  
  14. interface
  15.  
  16. uses SysUtils, Messages, Windows, Classes, Graphics, Menus, Controls, Forms,
  17.   StdCtrls, Mask;
  18.  
  19. const
  20.   MaxCustomExtents = MaxListSize;
  21.   MaxShortInt = High(ShortInt);
  22.  
  23. type
  24.   EInvalidGridOperation = class(Exception);
  25.  
  26.   { Internal grid types }
  27.   TGetExtentsFunc = function(Index: Longint): Integer of object;
  28.  
  29.   TGridAxisDrawInfo = record
  30.     EffectiveLineWidth: Integer;
  31.     FixedBoundary: Integer;
  32.     GridBoundary: Integer;
  33.     GridExtent: Integer;
  34.     LastFullVisibleCell: Longint;
  35.     FullVisBoundary: Integer;
  36.     FixedCellCount: Integer;
  37.     FirstGridCell: Integer;
  38.     GridCellCount: Integer;
  39.     GetExtent: TGetExtentsFunc;
  40.   end;
  41.  
  42.   TGridDrawInfo = record
  43.     Horz, Vert: TGridAxisDrawInfo;
  44.   end;
  45.  
  46.   TGridState = (gsNormal, gsSelecting, gsRowSizing, gsColSizing,
  47.     gsRowMoving, gsColMoving);
  48.  
  49.   { TInplaceEdit }
  50.   { The inplace editor is not intended to be used outside the grid }
  51.  
  52.   TCustomGrid = class;
  53.  
  54.   TInplaceEdit = class(TCustomMaskEdit)
  55.   protected
  56.     procedure CreateParams(var Params: TCreateParams); override;
  57.     procedure DblClick; override;
  58.     function EditCanModify: Boolean; override;
  59.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  60.     procedure KeyPress(var Key: Char); override;
  61.     procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  62.     procedure BoundsChanged; virtual;
  63.     procedure UpdateContents; virtual;
  64.     procedure WndProc(var Message: TMessage); override;
  65.     property  Grid: TCustomGrid;
  66.   public
  67.     constructor Create(AOwner: TComponent); override;
  68.     procedure Deselect;
  69.     procedure Hide;
  70.     procedure Invalidate;
  71.     procedure Move(const Loc: TRect);
  72.     function PosEqual(const Rect: TRect): Boolean;
  73.     procedure SetFocus;
  74.     procedure UpdateLoc(const Loc: TRect);
  75.     function Visible: Boolean;
  76.   end;
  77.  
  78.   { TCustomGrid }
  79.  
  80.   { TCustomGrid is an abstract base class that can be used to implement
  81.     general purpose grid style controls.  The control will call DrawCell for
  82.     each of the cells allowing the derived class to fill in the contents of
  83.     the cell.  The base class handles scrolling, selection, cursor keys, and
  84.     scrollbars.
  85.       DrawCell
  86.         Called by Paint. If DefaultDrawing is true the font and brush are
  87.         intialized to the control font and cell color.  The cell is prepainted
  88.         in the cell color and a focus rect is drawn in the focused cell after
  89.         DrawCell returns.  The state passed will reflect whether the cell is
  90.         a fixed cell, the focused cell or in the selection.
  91.       SizeChanged
  92.         Called when the size of the grid has changed.
  93.       BorderStyle
  94.         Allows a single line border to be drawn around the control.
  95.       Col
  96.         The current column of the focused cell (runtime only).
  97.       ColCount
  98.         The number of columns in the grid.
  99.       ColWidths
  100.         The width of each column (up to a maximum MaxCustomExtents, runtime
  101.         only).
  102.       DefaultColWidth
  103.         The default column width.  Changing this value will throw away any
  104.         customization done either visually or through ColWidths.
  105.       DefaultDrawing
  106.         Indicates whether the Paint should do the drawing talked about above in
  107.         DrawCell.
  108.       DefaultRowHeight
  109.         The default row height.  Changing this value will throw away any
  110.         customization done either visually or through RowHeights.
  111.       FixedCols
  112.         The number of non-scrolling columns.  This value must be at least one
  113.         below ColCount.
  114.       FixedRows
  115.         The number of non-scrolling rows.  This value must be at least one
  116.         below RowCount.
  117.       GridLineWidth
  118.         The width of the lines drawn between the cells.
  119.       LeftCol
  120.         The index of the left most displayed column (runtime only).
  121.       Options
  122.         The following options are available:
  123.           goFixedHorzLine:     Draw horizontal grid lines in the fixed cell area.
  124.           goFixedVertLine:     Draw veritical grid lines in the fixed cell area.
  125.           goHorzLine:          Draw horizontal lines between cells.
  126.           goVertLine:          Draw vertical lines between cells.
  127.           goRangeSelect:       Allow a range of cells to be selected.
  128.           goDrawFocusSelected: Draw the focused cell in the selected color.
  129.           goRowSizing:         Allows rows to be individually resized.
  130.           goColSizing:         Allows columns to be individually resized.
  131.           goRowMoving:         Allows rows to be moved with the mouse
  132.           goColMoving:         Allows columns to be moved with the mouse.
  133.           goEditing:           Places an edit control over the focused cell.
  134.           goAlwaysShowEditor:  Always shows the editor in place instead of
  135.                                waiting for a keypress or F2 to display it.
  136.           goTabs:              Enables the tabbing between columns.
  137.           goRowSelect:         Selection and movement is done a row at a time.
  138.       Row
  139.         The row of the focused cell (runtime only).
  140.       RowCount
  141.         The number of rows in the grid.
  142.       RowHeights
  143.         The hieght of each row (up to a maximum MaxCustomExtents, runtime
  144.         only).
  145.       ScrollBars
  146.         Determines whether the control has scrollbars.
  147.       Selection
  148.         A TGridRect of the current selection.
  149.       TopLeftChanged
  150.         Called when the TopRow or LeftCol change.
  151.       TopRow
  152.         The index of the top most row displayed (runtime only)
  153.       VisibleColCount
  154.         The number of columns fully displayed.  There could be one more column
  155.         partially displayed.
  156.       VisibleRowCount
  157.         The number of rows fully displayed.  There could be one more row
  158.         partially displayed.
  159.  
  160.     Protected members, for implementors of TCustomGrid descendents
  161.       DesignOptionBoost
  162.         Options mixed in only at design time to aid design-time editing.
  163.         Default = [goColSizing, goRowSizing], which makes grid cols and rows
  164.         resizeable at design time, regardless of the Options settings.
  165.       VirtualView
  166.         Controls the use of maximum screen clipping optimizations when the
  167.         grid window changes size.  Default = False, which means only the
  168.         area exposed by the size change will be redrawn, for less flicker.
  169.         VirtualView = True means the entire data area of the grid is redrawn
  170.         when the size changes.  This is required when the data displayed in
  171.         the grid is not bound to the number of rows or columns in the grid,
  172.         such as the dbgrid (a few grid rows displaying a view onto a million
  173.         row table).
  174.      }
  175.  
  176.   TGridOption = (goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
  177.     goRangeSelect, goDrawFocusSelected, goRowSizing, goColSizing, goRowMoving,
  178.     goColMoving, goEditing, goTabs, goRowSelect,
  179.     goAlwaysShowEditor, goThumbTracking);
  180.   TGridOptions = set of TGridOption;
  181.   TGridDrawState = set of (gdSelected, gdFocused, gdFixed);
  182.   TGridScrollDirection = set of (sdLeft, sdRight, sdUp, sdDown);
  183.  
  184.   TGridCoord = record
  185.     X: Longint;
  186.     Y: Longint;
  187.   end;
  188.  
  189.   TGridRect = record
  190.     case Integer of
  191.       0: (Left, Top, Right, Bottom: Longint);
  192.       1: (TopLeft, BottomRight: TGridCoord);
  193.   end;
  194.  
  195.   TSelectCellEvent = procedure (Sender: TObject; Col, Row: Longint;
  196.     var CanSelect: Boolean) of object;
  197.   TDrawCellEvent = procedure (Sender: TObject; Col, Row: Longint;
  198.     Rect: TRect; State: TGridDrawState) of object;
  199.  
  200.   TCustomGrid = class(TCustomControl)
  201.   protected
  202.     FGridState: TGridState;
  203.     FSaveCellExtents: Boolean;
  204.     DesignOptionsBoost: TGridOptions;
  205.     VirtualView: Boolean;
  206.     function CreateEditor: TInplaceEdit; virtual;
  207.     procedure CreateParams(var Params: TCreateParams); override;
  208.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  209.     procedure KeyPress(var Key: Char); override;
  210.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  211.       X, Y: Integer); override;
  212.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  213.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  214.       X, Y: Integer); override;
  215.     procedure AdjustSize(Index, Amount: Longint; Rows: Boolean); dynamic;
  216.     function BoxRect(ALeft, ATop, ARight, ABottom: Longint): TRect;
  217.     procedure DoExit; override;
  218.     function CellRect(ACol, ARow: Longint): TRect;
  219.     function CanEditAcceptKey(Key: Char): Boolean; dynamic;
  220.     function CanGridAcceptKey(Key: Word; Shift: TShiftState): Boolean; dynamic;
  221.     function CanEditModify: Boolean; dynamic;
  222.     function CanEditShow: Boolean; virtual;
  223.     function GetEditText(ACol, ARow: Longint): string; dynamic;
  224.     procedure SetEditText(ACol, ARow: Longint; const Value: string); dynamic;
  225.     function GetEditMask(ACol, ARow: Longint): string; dynamic;
  226.     function GetEditLimit: Integer; dynamic;
  227.     function GetGridWidth: Integer;
  228.     function GetGridHeight: Integer;
  229.     procedure HideEditor;
  230.     procedure ShowEditor;
  231.     procedure ShowEditorChar(Ch: Char);
  232.     procedure InvalidateEditor;
  233.     procedure MoveColumn(FromIndex, ToIndex: Longint);
  234.     procedure ColumnMoved(FromIndex, ToIndex: Longint); dynamic;
  235.     procedure MoveRow(FromIndex, ToIndex: Longint);
  236.     procedure RowMoved(FromIndex, ToIndex: Longint); dynamic;
  237.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  238.       AState: TGridDrawState); virtual; abstract;
  239.     procedure DefineProperties(Filer: TFiler); override;
  240.     procedure MoveColRow(ACol, ARow: Longint; MoveAnchor, Show: Boolean);
  241.     function SelectCell(ACol, ARow: Longint): Boolean; virtual;
  242.     procedure SizeChanged(OldColCount, OldRowCount: Longint); dynamic;
  243.     function Sizing(X, Y: Integer): Boolean;
  244.     procedure ScrollData(DX, DY: Integer);
  245.     procedure InvalidateCell(ACol, ARow: Longint);
  246.     procedure InvalidateCol(ACol: Longint);
  247.     procedure InvalidateRow(ARow: Longint);
  248.     procedure TopLeftChanged; dynamic;
  249.     procedure TimedScroll(Direction: TGridScrollDirection); dynamic;
  250.     procedure Paint; override;
  251.     procedure ColWidthsChanged; dynamic;
  252.     procedure RowHeightsChanged; dynamic;
  253.     procedure DeleteColumn(ACol: Longint);
  254.     procedure DeleteRow(ARow: Longint);
  255.     procedure UpdateDesigner;
  256.     property BorderStyle: TBorderStyle default bsSingle;
  257.     property Col: Longint;
  258.     property Color default clWindow;
  259.     property ColCount: Longint default 5;
  260.     property ColWidths[Index: Longint]: Integer;
  261.     property DefaultColWidth: Integer default 64;
  262.     property DefaultDrawing: Boolean default True;
  263.     property DefaultRowHeight: Integer default 24;
  264.     property EditorMode: Boolean;
  265.     property FixedColor: TColor default clBtnFace;
  266.     property FixedCols: Integer default 1;
  267.     property FixedRows: Integer default 1;
  268.     property GridHeight: Integer;
  269.     property GridLineWidth: Integer default 1;
  270.     property GridWidth: Integer;
  271.     property HitTest: TPoint;
  272.     property InplaceEditor: TInplaceEdit;
  273.     property LeftCol: Longint;
  274.     property Options: TGridOptions default [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect];
  275.     property ParentColor default False;
  276.     property Row: Longint;
  277.     property RowCount: Longint default 5;
  278.     property RowHeights[Index: Longint]: Integer;
  279.     property ScrollBars: TScrollStyle default ssBoth;
  280.     property Selection: TGridRect;
  281.     property TabStops[Index: Longint]: Boolean;
  282.     property TopRow: Longint;
  283.     property VisibleColCount: Integer;
  284.     property VisibleRowCount: Integer;
  285.   public
  286.     constructor Create(AOwner: TComponent); override;
  287.     destructor Destroy; override;
  288.     function MouseCoord(X, Y: Integer): TGridCoord;
  289.   published
  290.     property TabStop default True;
  291.   end;
  292.  
  293.   { TDrawGrid }
  294.  
  295.   { A grid relies on the OnDrawCell event to display the cells.
  296.      CellRect
  297.        This method returns control relative screen coordinates of the cell or
  298.        an empty rectangle if the cell is not visible.
  299.      EditorMode
  300.        Setting to true shows the editor, as if the F2 key was pressed, when
  301.        goEditing is turned on and goAlwaysShowEditor is turned off.
  302.      MouseToCell
  303.        Takes control relative screen X, Y location and fills in the column and
  304.        row that contain that point.
  305.      OnColumnMoved
  306.        Called when the user request to move a column with the mouse when
  307.        the goColMoving option is on.
  308.      OnDrawCell
  309.        This event is passed the same information as the DrawCell method
  310.        discussed above.
  311.      OnGetEditMask
  312.        Called to retrieve edit mask in the inplace editor when goEditing is
  313.        turned on.
  314.      OnGetEditText
  315.        Called to retrieve text to edit when goEditing is turned on.
  316.      OnRowMoved
  317.        Called when the user request to move a row with the mouse when
  318.        the goRowMoving option is on.
  319.      OnSetEditText
  320.        Called when goEditing is turned on to reflect changes to the text
  321.        made by the editor.
  322.      OnTopLeftChanged
  323.        Invoked when TopRow or LeftCol change. }
  324.  
  325.   TGetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; var Value: string) of object;
  326.   TSetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; const Value: string) of object;
  327.   TMovedEvent = procedure (Sender: TObject; FromIndex, ToIndex: Longint) of object;
  328.  
  329.   TDrawGrid = class(TCustomGrid)
  330.   protected
  331.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  332.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  333.       AState: TGridDrawState); override;
  334.     function GetEditMask(ACol, ARow: Longint): string; override;
  335.     function GetEditText(ACol, ARow: Longint): string; override;
  336.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  337.     function SelectCell(ACol, ARow: Longint): Boolean; override;
  338.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  339.     procedure TopLeftChanged; override;
  340.   public
  341.     function CellRect(ACol, ARow: Longint): TRect;
  342.     procedure MouseToCell(X, Y: Integer; var ACol, ARow: Longint);
  343.     property Canvas;
  344.     property Col;
  345.     property ColWidths;
  346.     property EditorMode;
  347.     property GridHeight;
  348.     property GridWidth;
  349.     property LeftCol;
  350.     property Selection;
  351.     property Row;
  352.     property RowHeights;
  353.     property TabStops;
  354.     property TopRow;
  355.   published
  356.     property Align;
  357.     property BorderStyle;
  358.     property Color;
  359.     property ColCount;
  360.     property Ctl3D;
  361.     property DefaultColWidth;
  362.     property DefaultRowHeight;
  363.     property DefaultDrawing;
  364.     property DragCursor;
  365.     property DragMode;
  366.     property Enabled;
  367.     property FixedColor;
  368.     property FixedCols;
  369.     property RowCount;
  370.     property FixedRows;
  371.     property Font;
  372.     property GridLineWidth;
  373.     property Options;
  374.     property ParentColor;
  375.     property ParentCtl3D;
  376.     property ParentFont;
  377.     property ParentShowHint;
  378.     property PopupMenu;
  379.     property ScrollBars;
  380.     property ShowHint;
  381.     property TabOrder;
  382.     property TabStop;
  383.     property Visible;
  384.     property VisibleColCount;
  385.     property VisibleRowCount;
  386.     property OnClick;
  387.     property OnColumnMoved: TMovedEvent;
  388.     property OnDblClick;
  389.     property OnDragDrop;
  390.     property OnDragOver;
  391.     property OnDrawCell: TDrawCellEvent;
  392.     property OnEndDrag;
  393.     property OnEnter;
  394.     property OnExit;
  395.     property OnGetEditMask: TGetEditEvent;
  396.     property OnGetEditText: TGetEditEvent;
  397.     property OnKeyDown;
  398.     property OnKeyPress;
  399.     property OnKeyUp;
  400.     property OnMouseDown;
  401.     property OnMouseMove;
  402.     property OnMouseUp;
  403.     property OnRowMoved: TMovedEvent;
  404.     property OnSelectCell: TSelectCellEvent;
  405.     property OnSetEditText: TSetEditEvent;
  406.     property OnStartDrag;
  407.     property OnTopLeftChanged: TNotifyEvent;
  408.   end;
  409.  
  410.   { TStringGrid }
  411.  
  412.   { TStringGrid adds to TDrawGrid the ability to save a string and associated
  413.     object (much like TListBox).  It also adds to the DefaultDrawing the drawing
  414.     of the string associated with the current cell.
  415.       Cells
  416.         A ColCount by RowCount array of strings which are associated with each
  417.         cell.  By default, the string is drawn into the cell before OnDrawCell
  418.         is called.  This can be turned off (along with all the other default
  419.         drawing) by setting DefaultDrawing to false.
  420.       Cols
  421.         A TStrings object that contains the strings and objects in the column
  422.         indicated by Index.  The TStrings will always have a count of RowCount.
  423.         If a another TStrings is assigned to it, the strings and objects beyond
  424.         RowCount are ignored.
  425.       Objects
  426.         A ColCount by Rowcount array of TObject's associated with each cell.
  427.         Object put into this array will *not* be destroyed automatically when
  428.         the grid is destroyed.
  429.       Rows
  430.         A TStrings object that contains the strings and objects in the row
  431.         indicated by Index.  The TStrings will always have a count of ColCount.
  432.         If a another TStrings is assigned to it, the strings and objects beyond
  433.         ColCount are ignored. }
  434.  
  435.   TStringGrid = class;
  436.  
  437.   TStringGridStrings = class(TStrings)
  438.   protected
  439.     procedure Clear; override;
  440.     function Add(const S: string): Integer; override;
  441.     function Get(Index: Integer): string; override;
  442.     function GetCount: Integer; override;
  443.     function GetObject(Index: Integer): TObject; override;
  444.     procedure Put(Index: Integer; const S: string); override;
  445.     procedure PutObject(Index: Integer; AObject: TObject); override;
  446.     procedure SetUpdateState(Updating: Boolean); override;
  447.   public
  448.     constructor Create(AGrid: TStringGrid; AIndex: Longint);
  449.     procedure Assign(Source: TPersistent); override;
  450.     procedure Delete(Index: Integer); override;
  451.     procedure Insert(Index: Integer; const S: string); override;
  452.   end;
  453.  
  454.  
  455.   TStringGrid = class(TDrawGrid)
  456.   protected
  457.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  458.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  459.       AState: TGridDrawState); override;
  460.     function GetEditText(ACol, ARow: Longint): string; override;
  461.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  462.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  463.   public
  464.     constructor Create(AOwner: TComponent); override;
  465.     destructor Destroy; override;
  466.     property Cells[ACol, ARow: Integer]: string;
  467.     property Cols[Index: Integer]: TStrings;
  468.     property Objects[ACol, ARow: Integer]: TObject;
  469.     property Rows[Index: Integer]: TStrings;
  470.   end;
  471.  
  472. implementation
  473.